package com.vichat.task;

import com.alibaba.dubbo.container.spring.SpringContainer;
import com.vichat.common.util.DateUtil;
import com.vichat.online.service.IOnlineService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import com.vichat.task.service.ITask;

/**
 * Created by wangluyin on 2017/1/24.
 * 每天登录时长统计到月表处理，执行时间晚上2点
 */
@Component("OnlineUserMonthLogTask")
public class OnlineUserMonthLogTask implements ITask{

    private static transient final Logger logger = LoggerFactory.getLogger(OnlineUserMonthLogTask.class);

    @Override
    public String doTask(long cfgTaskId) throws Exception {

        ApplicationContext ctx = SpringContainer.getContext();

        IOnlineService onlineService = (IOnlineService) ctx.getBean("onlineService", IOnlineService.class);
        String yesterDay = DateUtil.getYesterday("yyyyMMdd");
        String yesterDayMonth = DateUtil.getYesterday("yyyyMM");
        boolean isExist = onlineService.isExistOnlineUserMLog(yesterDayMonth,yesterDay);
        if(!isExist)
            onlineService.processOnlineUserMLog(yesterDayMonth,yesterDay);
        else
            throw new Exception("异常！登录时长数据("+yesterDay+")已经生成！");
        return null;
    }
}
